package cn.kgc.ssm.web.service.impl;

import cn.kgc.ssm.web.entity.Slide;
import cn.kgc.ssm.web.mapper.SlideMapper;
import cn.kgc.ssm.web.response.ResponseResult;
import cn.kgc.ssm.web.service.SlideService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/8/1
 * @description
 */
@Service
public class SlideServiceImpl implements SlideService {
    @Autowired
    private SlideMapper slideMapper;

    @Override
    public ResponseResult slidePage(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);

        List<Slide> slides =  slideMapper.selectList();
        long total = new PageInfo<Slide>(slides).getTotal();

        return ResponseResult.success().data(slides).count(total);
    }

    @Override
    public ResponseResult addUploadInfo(Slide slide) {

        slideMapper.addUploadInfo(slide);
        return ResponseResult.success();
    }


    @Transactional
    @Override
    public ResponseResult deletePicture(String uuid) {


        slideMapper.deletePicture(uuid);

        File file = new File("D:\\upload\\slides\\" + uuid);
        if (file.exists()){
            file.delete();
        }


        return ResponseResult.success();
    }

    @Override
    public ResponseResult topPicture(Integer id) {

        slideMapper.updatePictureTime(id);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult fontPictureList(Integer number) {

      List<Slide> slides =   slideMapper.selectFontPicture(number);
        return ResponseResult.success().data(slides);
    }
}
